package pl.mess.core.tags.elements;

import pl.mess.core.tags.ifc.MESSTagNameIfc;
import pl.mess.core.tags.writer.MESSXmlWriter;

import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

/**
 * Created by IntelliJ IDEA.
 * User: gibffe
 * Date: Mar 11, 2006
 * Time: 3:52:46 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class MESSBaseTag extends BodyTagSupport implements MESSTagNameIfc {


    private PageContext context = null;

    private MESSXmlWriter writer = null;



    /**
     * doStartTag method implementation
     * @return int value
     * @throws JspException
     */
    public int doStartTag() throws JspException {

        // create xml writer instance, for tag to use
        this.writer = new MESSXmlWriter(this.context.getOut());

        this.writeTagStart(this);
        return this.evalBody();
    }

    /**
     * Save page context for later usage in MESS tags.
     * @param pageContext
     */
    public void setPageContext(PageContext pageContext) {

        this.context = pageContext;
    }

    /**
     * Return page context variable
     * @return context
     */
    public PageContext getPageContext() {

        return this.context;
    }

    /**
     * Return EVAL_BODY_INCLUDE status code
     * @return int
     */
    public int evalBody() {
        return EVAL_BODY_INCLUDE;
    }

    /**
     * Return EVAL_PAGE status code
     * @return int
     */
    public int evalPage() {
        return EVAL_PAGE;
    }

    /**
     * Write xml tag header with attributes
     * @param tag
     * @return 
     */
    public int writeTagStart(MESSBaseTag tag) {
        return this.writer.writeTagStart(tag);
    }

    public int writeTagStart() {
        return this.writer.writeTagStart(this);
    }

    /**
     * Write tag ending sequence
     * @param tag
     * @return
     */
    public int writeTagEnd(MESSBaseTag tag) {
        return this.writer.writeTagEnd(tag);
    }

    public int writeTagEnd() {
        return this.writer.writeTagEnd(this);
    }



    /**
     * doEndTag method implementation
     * @return int value
     * @throws JspException
     */
    public int doEndTag() throws JspException {

        this.writeTagEnd(this);
        return this.evalPage();
    }

    // property: writer
    public MESSXmlWriter getWriter() {
        return this.writer;
    }

    public void setWriter(MESSXmlWriter writer) {
        this.writer = writer;
    }


}
